
library(logr)

log_open()

log_print("Replication code: Parties and (de)liberalization")

# data
libdata = readRDS("libdata")

log_print(libdata)

# packages
library(lme4)
library(lmerTest)
library(stargazer)
library(sjPlot)
library(vtable)

# Model 1
m1 = lmer(lib ~ instcons + vturn + duration + succession + openc + unemp + 
              elderly + deindust + ud + debt + deficit +
              gdp_head + realgdpgr + inflation + cs1 + cs2 + gov_left +
              (1|countryn) + (1|govern) + (1|year), libdata)
log_print(m1)

# Model 2
m2 = lmer(lib ~ instcons + vturn + duration + succession + openc + unemp + 
            elderly + deindust + ud + debt + deficit +
            gdp_head + realgdpgr + inflation + cs1 + cs2 + soci +
            (1|countryn) + (1|govern) + (1|year), libdata)
log_print(m2)
  
# Model 3
m3 = lmer(lib ~ instcons + vturn + duration + succession + openc + unemp + 
              elderly + deindust + ud + debt + deficit +
              gdp_head + realgdpgr + inflation + cs1 + cs2 + chri +
              (1|countryn) + (1|govern) + (1|year), libdata)
log_print(m3)

# Model 4
m4 = lmer(lib ~ instcons + vturn + duration + succession + openc + unemp + 
               elderly + deindust + ud + debt + deficit +
               gdp_head + realgdpgr + inflation + cs1 + cs2 + cabinet_econ +
               (1|countryn) + (1|govern) + (1|year), libdata)
log_print(m4)

# Table 2
tab_model(m1, m2, m3, m4, p.val = "kr", file = "table_2.html", show.ci = F, 
          show.p = T, digits = 3)

# Model 5
m5 = lmer(delib ~ instcons +  vturn + duration + succession + openc + unemp + 
              elderly + deindust + debt + deficit + 
              gdp_head + realgdpgr + inflation + cs1 + cs2 + gov_left +
              (1|countryn) + (1|govern) + (1|year), libdata)
log_print(m5)

# Model 6
m6 = lmer(delib ~ instcons + vturn + duration + succession + openc + unemp + 
               elderly + deindust + debt + deficit +
               gdp_head + realgdpgr + inflation + cs1 + cs2 + soci +
               (1|countryn) + (1|govern) + (1|year), libdata)
log_print(m6)

# Model 7
m7 = lmer(delib ~ instcons + vturn + duration + succession + openc + unemp + 
               elderly + deindust + debt + deficit +
               gdp_head + realgdpgr + inflation + cs1 + cs2 + chri +
               (1|countryn) + (1|govern) + (1|year), libdata)
log_print(m7)
  
# Model 8
m8 = lmer(delib ~ instcons + vturn + duration + succession + openc + unemp + 
               elderly + deindust + debt + deficit +
               gdp_head + realgdpgr + inflation + cs1 + cs2 + cabinet_econ +
               (1|countryn) + (1|govern) + (1|year), libdata)
log_print(m8)

# Table 3
log_print(tab_model(m5, m6, m7, m8, p.val = "kr", file = "table_3.html", 
          show.ci = F, show.p = T, digits = 3))

# split
libdata_73_92 = subset(libdata, year<1993)
log_print(libdata_73_92)
libdata_93_12 = subset(libdata, year>1992)
log_print(libdata_93_12)

# Model 9
m9 = lmer(lib ~ instcons + vturn + duration + succession + openc + unemp + 
                 elderly + deindust + ud + debt + deficit +
                 gdp_head + realgdpgr + inflation + cs1 + cs2 + gov_left +
                 (1|countryn) + (1|govern) + (1|year), libdata_73_92)
log_print(m9)

# Model 10
m10 = lmer(lib ~ instcons + vturn + duration + succession + openc + unemp + 
                 elderly + deindust + ud + debt + deficit +
                 gdp_head + realgdpgr + inflation + cs1 + cs2 + soci +
                 (1|countryn) + (1|govern) + (1|year), libdata_73_92)
log_print(m10)

# Model 11
m11 = lmer(lib ~ instcons + vturn + duration + succession + openc + unemp + 
                 elderly + deindust + ud + debt + deficit +
                 gdp_head + realgdpgr + inflation + cs1 + cs2 + chri +
                 (1|countryn) + (1|govern) + (1|year), libdata_73_92)
log_print(m11)

# Model 12
m12 = lmer(lib ~ instcons + vturn + duration + succession + openc + unemp + 
                  elderly + deindust + ud + debt + deficit +
                  gdp_head + realgdpgr + inflation + cs1 + cs2 + cabinet_econ +
                  (1|countryn) + (1|govern) + (1|year), libdata_73_92)
log_print(m12)

# Table 4a
tab_model(m9, m10, m11, m12, p.val = "kr", file = "table_4a.html", 
          show.ci = F, show.p = T, digits = 3)

# Model 13
m13 = lmer(delib ~ instcons +  vturn + duration + succession + openc + unemp + 
                 elderly + deindust + ud + debt + deficit +
                 gdp_head + realgdpgr + inflation + cs1 + cs2 + gov_left +
                 (1|countryn) + (1|govern) + (1|year), libdata_73_92)
log_print(m13)

# Model 14
m14 = lmer(delib ~ instcons + vturn + duration + succession + openc + unemp + 
                  elderly + deindust + ud + debt + deficit +
                  gdp_head + realgdpgr + inflation + cs1 + cs2 + soci +
                  (1|countryn) + (1|govern) + (1|year), libdata_73_92)
log_print(m14)

# Model 15
m15 = lmer(delib ~ instcons + vturn + duration + succession + openc + unemp + 
                  elderly + deindust + ud + debt + deficit +
                  gdp_head + realgdpgr + inflation + cs1 + cs2 + chri +
                  (1|countryn) + (1|govern) + (1|year), libdata_73_92)
log_print(m15)

# Model 16
m16 = lmer(delib ~ instcons + vturn + duration + succession + openc + unemp + 
               elderly + deindust + ud + debt + deficit +
               gdp_head + realgdpgr + inflation + cs1 + cs2 + cabinet_econ +
               (1|countryn) + (1|govern) + (1|year), libdata_73_92)
log_print(m16)

# Table 4b
tab_model(m13, m14, m15, m16, p.val = "kr", file = "table_4b.html", 
          show.ci = F, show.p = T, digits = 3)

# Model 17
m17 = lmer(lib ~ instcons + vturn + duration + succession + openc + unemp + 
                 elderly + deindust  + ud + debt  + deficit  +
                 gdp_head  + realgdpgr + inflation + cs1 + cs2 + gov_left +
                 (1|countryn) + (1|govern) + (1|year), libdata_93_12)
log_print(m17)

# Model 18
m18 = lmer(lib ~ instcons + vturn + duration + succession + openc  + unemp + 
                 elderly + deindust  + ud + debt  + deficit  +
                 gdp_head  + realgdpgr + inflation + cs1 + cs2 + soci +
                 (1|countryn) + (1|govern) + (1|year), libdata_93_12)
log_print(m18)

# Model 19
m19 = lmer(lib ~ instcons + vturn + duration + succession + openc + unemp + 
                 elderly + deindust + ud + debt + deficit +
                 gdp_head + realgdpgr + inflation + cs1 + cs2 + chri +
                 (1|countryn) + (1|govern) + (1|year), libdata_93_12)
log_print(m19)

# Model 20
m20 = lmer(lib ~ instcons + vturn + duration + succession + openc + unemp + 
                  elderly + deindust + ud + debt + deficit +
                  gdp_head + realgdpgr + inflation + cs1 + cs2 + cabinet_econ +
                  (1|countryn) + (1|govern) + (1|year), libdata_93_12)
log_print(m20)

# Table 4c
tab_model(m17, m18, m19, m20, p.val = "kr", file = "table_4c.html",
          show.ci = F, show.p = T, digits = 3)

# Model 21
m21 = lmer(delib ~ instcons +  vturn + duration + succession + openc  + unemp + 
                 elderly + deindust + debt + deficit  +
                 gdp_head  + realgdpgr + inflation + cs1 + cs2 + gov_left +
                 (1|countryn) + (1|govern) + (1|year), libdata_93_12)
log_print(m21)

# Model 22
m22 = lmer(delib ~ instcons + vturn + duration + succession + openc  + unemp + 
                  elderly + deindust + ud + debt  + deficit +
                  gdp_head  + realgdpgr + inflation + cs1 + cs2 + soci +
                  (1|countryn) + (1|govern) + (1|year), libdata_93_12)
log_print(m22)

# Model 23
m23 = lmer(delib ~ instcons + vturn + duration + succession + openc + unemp + 
                  elderly + deindust + debt + deficit +
                  gdp_head + realgdpgr + inflation + cs1 + cs2 + chri +
                  (1|countryn) + (1|govern) + (1|year), libdata_93_12)
log_print(m23)

# Model 24
m24 = lmer(delib ~ instcons + vturn + duration + succession + openc + unemp + 
               elderly + deindust + debt + gdp_head  + 
               inflation + cs1 + cs2 + cabinet_econ +
               (1|countryn) + (1|govern) + (1|year), libdata_93_12)
log_print(m24)

tab_model(m21, m22, m23, m24, p.val = "kr", file = "table_4d.html", 
          show.ci = F, show.p = T, digits = 3)

# Table A
labels=c("Liberalization","Deliberalization","Institutional constraints",
        "Voter turnout","Government duration","Government succession",
        "Capital openness","Unemployment rate","Elder population",
        "Deindustrialization","Union density","Public debt","Public deficit",
        "GDP per capita","GDP growth","Inflation","Left cabinet strength", 
        "Social dem. cabinet","Christian dem. cabinet","Conservative cabinet",  
        "Cabinet econ. position")
sumtable(vars = c("lib","delib","instcons","vturn","duration","succession", 
                  "openc","unemp","elderly","deindust","ud","debt","deficit", 
                  "gdp_head","realgdpgr","inflation","gov_left","soci","chri", 
                  "cons","cabinet_econ"), file='table_A', libdata, labels = labels)

# Model 25
m25 = lmer(delib ~ instcons + vturn + duration + succession + openc + unemp + 
                   elderly + deindust + debt + deficit +
                   gdp_head + realgdpgr + inflation + cs1 + cs2 + cons +
                   (1|countryn) + (1|govern) + (1|year), libdata_93_12)
log_print(m25)

# Table B
tab_model(m25, p.val = "kr", file = "table_B.html", 
          show.ci = F, show.p = T, digits = 3)

# Model 26
m26 = lmer(libtax ~ instcons + vturn + duration + succession + openc + unemp + 
                elderly + deindust + ud + debt + deficit +
                gdp_head + realgdpgr + inflation + cs1 + cs2 + gov_left +
                (1|countryn) + (1|govern) + (1|year), libdata)
log_print(m26)

# Model 27
m27 = lmer(libtax ~ instcons + vturn + duration + succession + openc + unemp + 
                elderly + deindust + ud + debt + deficit +
                gdp_head + realgdpgr + inflation + cs1 + cs2 + cabinet_econ +
                (1|countryn) + (1|govern) + (1|year), libdata)
log_print(m27)

# Model 28
m28 = lmer(libtax ~ instcons + vturn + duration + succession + openc + unemp + 
               elderly + deindust + ud + debt + deficit +
               gdp_head + realgdpgr + inflation + cs1 + cs2 + soci +
               (1|countryn) + (1|govern) + (1|year), libdata)
log_print(m28)

# Model 29
m29 = lmer(libtax ~ instcons + vturn + duration + succession + openc + unemp + 
               elderly + deindust + ud + debt + deficit +
               gdp_head + realgdpgr + inflation + cs1 + cs2 + chri +
               (1|countryn) + (1|govern) + (1|year), libdata)
log_print(m29)

# Table C
tab_model(m26, m27, m28, m29, p.val = "kr", file = "table_C.html", 
          show.ci = F, show.p = T, digits = 3)

# Model 30
m30 = lmer(libepl ~ instcons + vturn + duration + succession + openc + unemp + 
               elderly + deindust + ud + debt + deficit + 
               gdp_head + realgdpgr + inflation + cs1 + cs2 + gov_left +
               (1|countryn) + (1|govern) + (1|year), libdata)
log_print(m30)

# Model 31
m31 = lmer(libepl ~ instcons + vturn + duration + succession + openc + unemp + 
               elderly + deindust + ud + debt + deficit + 
               gdp_head + realgdpgr + inflation + cs1 + cs2 + cabinet_econ + 
               (1|countryn) + (1|govern) + (1|year), libdata)
log_print(m31)

# Model 32
m32 = lmer(libepl ~ instcons + vturn + duration + succession + openc + unemp + 
               elderly + deindust + ud + debt + deficit + 
               gdp_head + realgdpgr + inflation + cs1 + cs2 + soci +
               (1|countryn) + (1|govern) + (1|year), libdata)
log_print(m32)

# Model 33
m33 = lmer(libepl ~ instcons + vturn + duration + succession + openc + unemp + 
               elderly + deindust + ud + debt + deficit + 
               gdp_head + realgdpgr + inflation + cs1 + cs2 + chri +
               (1|countryn) + (1|govern) + (1|year), libdata)
log_print(m33)

# Table D
tab_model(m30, m31, m32, m33, p.val = "kr", file = "table_D.html", 
          show.ci = F, show.p = T, digits = 3)

# Model 34
m34 = lmer(libneb ~ instcons + vturn + duration + succession + openc + unemp + 
               elderly + deindust + ud + debt + deficit +
               gdp_head + realgdpgr + inflation + cs1 + cs2 + gov_left +
               (1|countryn) + (1|govern) + (1|year), libdata)
log_print(m34)

# Model 35
m35 = lmer(libneb ~  vturn + duration + succession + openc + unemp + 
               elderly + deindust + ud + debt + deficit +
               gdp_head + realgdpgr + inflation + cs1 + cs2 + cabinet_econ +
               (1|countryn) + (1|govern) + (1|year), libdata)
log_print(m35)

# Model 36
m36 = lmer(libneb ~ instcons + vturn + duration + succession + openc + unemp + 
               elderly + deindust + ud + debt + deficit +
               gdp_head + realgdpgr + inflation + cs1 + cs2 + soci +
               (1|countryn) + (1|govern) + (1|year), libdata)
log_print(m36)

# Model 37
m37 = lmer(libneb ~ instcons + vturn + duration + succession + openc + unemp + 
               elderly + deindust + ud + debt + deficit +
               gdp_head + realgdpgr + inflation + cs1 + cs2 + chri +
               (1|countryn) + (1|govern) + (1|year), libdata)
log_print(m37)

# Table E
tab_model(m34, m35, m36, m37, p.val = "kr", file = "table_E.html", 
          show.ci = F, show.p = T, digits = 3)

# Model 38
m38 = lmer(delibpri ~ instcons + vturn + duration + succession + openc + unemp + 
             elderly + deindust + ud + debt + deficit + 
             gdp_head + realgdpgr + inflation + cs1 + cs2 + gov_left +
             (1|countryn) + (1|govern) + (1|year), libdata)
log_print(m38)

# Model 39
m39 = lmer(delibpri ~ instcons + vturn + duration + succession + openc + unemp + 
             elderly + deindust + ud + debt + deficit + 
             gdp_head + realgdpgr + inflation + cs1 + cs2 + cabinet_econ + 
             (1|countryn) + (1|govern) + (1|year), libdata)
log_print(m39)

# Model 40
m40 = lmer(delibpri ~ instcons + vturn + duration + succession + openc + unemp + 
             elderly + deindust + ud + debt + deficit + 
             gdp_head + realgdpgr + inflation + cs1 + cs2 + soci +
             (1|countryn) + (1|govern) + (1|year), libdata)
log_print(m40)

# Model 41
m41 = lmer(delibpri ~ instcons + vturn + duration + succession + openc + unemp + 
             elderly + deindust + ud + debt + deficit + 
             gdp_head + realgdpgr + inflation + cs1 + cs2 + chri +
             (1|countryn) + (1|govern) + (1|year), libdata)
log_print(m41)

# Table F
tab_model(m38, m39, m40, m41, p.val = "kr", file = "table_F.html", 
          show.ci = F, show.p = T, digits = 3)

log_close()

